package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import domain.TeloReport;

public class TeloDAO extends HibernateDaoSupport {
	public int iidCount(String internal_id){
		Session s = getSession();
		Long l = (Long)getSession().createQuery("select count(*) from TeloReport where internal_id = '" + internal_id + "'").uniqueResult();
		s.close();
		return l.intValue();
	}
	
	public TeloReport getTRByID(Integer id){
		return (TeloReport)getHibernateTemplate().get(TeloReport.class,id);
	}
	
	public void save(TeloReport t) {
		getHibernateTemplate().saveOrUpdate(t);
	}
	
	@SuppressWarnings("unchecked")
	public List<TeloReport> listHistory(String internal_id){
		List<TeloReport> lItem = new ArrayList<TeloReport>();
		String hql = "from TeloReport where internal_id='"+internal_id+"'";
		Session s = getSession();
		Query query = s.createQuery(hql);
		lItem = query.list();
		s.close();
		return lItem;
	}
	
	@SuppressWarnings("unchecked")
	public List<TeloReport> listItem(TeloReport tr,int start, int size,String startDate, String endDate){
		List<TeloReport> lItem = new ArrayList<TeloReport>();
		String hql = "from TeloReport where 1=1";
		boolean bn = false;
		if (tr != null){
			String name = tr.getName();
			if ((name != null) && (!name.equals(""))) {
				hql = hql + " and name like :name";
				bn = true;
			}
			String iid = tr.getInternal_id();
			if (iid != null && !iid.equals("")){
				hql = hql + " and internal_id like '%" + iid.replaceAll("[*]", "_")
				+ "%'";
			}
		}
		if ((startDate != null) && (!startDate.equals(""))) {
			hql = hql + " and report_date >= '" + startDate + "'";
		}
		if ((endDate != null) && (!endDate.equals(""))) {
			hql = hql + " and report_date <= '" + endDate + "'";
		}
		hql = hql + " order by report_date desc";
		Session s = getSession();
		Query query = s.createQuery(hql);
		if (bn) {
			query.setString("name", "%" + tr.getName().replaceAll("[*]", "_")
					+ "%");
		}
		query.setFirstResult(start);
		query.setMaxResults(size);
		lItem = query.list();
		s.close();
		return lItem;
	}
	
	public int pageTotal(TeloReport tr,int pageSize, String startDate,
			String endDate) {
		String hql = "select count(*) from TeloReport where 1=1";
		boolean bn = false;
		if (tr != null){
			String name = tr.getName();
			if ((name != null) && (!name.equals(""))) {
				hql = hql + " and name like :name";
				bn = true;
			}
			String iid = tr.getInternal_id();
			if (iid != null && !iid.equals("")){
				hql = hql + " and internal_id like '%" + iid.replaceAll("[*]", "_")
				+ "%'";
			}
		}
		if ((startDate != null) && (!startDate.equals(""))) {
			hql = hql + " and report_date >= '" + startDate + "'";
		}
		if ((endDate != null) && (!endDate.equals(""))) {
			hql = hql + " and report_date <= '" + endDate + "'";
		}
		Session s = getSession();
		Query query = s.createQuery(hql);
		if (bn) {
			query.setString("name", "%" + tr.getName().replaceAll("[*]", "_")
					+ "%");
		}
		int num = ((Number) query.iterate().next()).intValue();
		int p = (int) Math.ceil((double)num / pageSize);
		s.close();
		return p;
	}
}
